using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

namespace Nate.GUI.Canvas.Component
{
    public class VertexMove : IComponent
    {
        public VertexMove(NavMesh navmesh_canvas)
        {
            _navmesh_canvas = navmesh_canvas;
            Vertex = null;
        }

        public void InitializeComponent()
        {
            _navmesh_canvas.MouseClick += new MouseEventHandler(_MouseClick);
            _navmesh_canvas.MouseMove += new MouseEventHandler(_MouseMove);
            _navmesh_canvas.MouseDown += new MouseEventHandler(_MouseDown);
            _navmesh_canvas.MouseUp += new MouseEventHandler(_MouseUp);
        }

        public void Tick()
        {
        }

        private void _MouseClick(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Right == e.Button)
            {
                Vertex = null;

                // Split an edge
                // ...
            }
        }

        private void _MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left == e.Button)
            {
                // Grab a vertex

                NavMesh.Vertex temp = null;
                _navmesh_canvas.Bucket.Find(new PointF((float)e.Location.X, (float)e.Location.Y), out temp);
                Vertex = temp;
            }
        }

        private void _MouseUp(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left == e.Button)
            {
                // Release a grabbed vertex

                Vertex = null;
            }
        }

        private void _MouseMove(object sender, MouseEventArgs e)
        {
            if (null != Vertex)
            {
                // Move a grabbed vertex

                Vertex.Point = new PointF((float)e.Location.X, (float)e.Location.Y);
                _navmesh_canvas.RefreshCanvas();
            }
        }

        private NavMesh.Vertex Vertex
        {
            get
            {
                return _vertex;
            }
            set
            {
                if (null != _vertex)
                {
                    _vertex.Highlighted = false;
                    _navmesh_canvas.XML.WriteToFile(NavMesh.Xml.Constants.XmlFile, _navmesh_canvas.Bucket);
                }
                if (null != value)
                {
                    value.Highlighted = true;
                }

                _vertex = value;
            }
        }


        private NavMesh _navmesh_canvas;
        private NavMesh.Vertex _vertex;
    }
}